package cn.neusoft.luofa603.yuejian_29.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import cn.neusoft.luofa603.yuejian_29.bean.UserReg;
import cn.neusoft.luofa603.yuejian_29.Model;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.databinding.ActivityRegisterBinding;

public class RegisterActivity extends AppCompatActivity {
   ActivityRegisterBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        binding.setRegister(new Register());
    }

    public class Register {

        public void onClickReg() {
            if(binding.pwd1.getText().toString().equals(binding.pwd2.getText().toString())) {

                Model.with(Model.userService.Register(binding.tell.getText().toString(), binding.pwd1.getText().toString()))
                        .positive(new Model.Positive<UserReg>() {
                            @Override
                            public void onPositive(UserReg data) {

                                //http://www.jb51.net/article/37204.htm
                                //5、Log.e为红色，可以想到error错误，这里仅显示红色的错误信息，这些错误就需要我们认真的分析，查看栈的信息了。
//                                Log.e("----------------",data.toString());
                                if (data.getId() == null) {
                                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                } else {

                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                                    intent.putExtra("phone", binding.tell.getText().toString());
                                    intent.putExtra("pwd", binding.pwd1.getText().toString());
                                    setResult(RESULT_OK,intent);
                                    finish();
//                                    startActivityForResult(intent,1);


                                }
                            }


                        })
                        .failure(new Model.Failure() {
                            @Override
                            public void onFailure() {
                                Toast.makeText(RegisterActivity.this,"联网失败！",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .request();

            }else{

                Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }


        }
    }
}


